<?php
session_start();
include ('headerforsuggestion.php');
 include('databaseconnection.php');
 $sugg=$_POST['butt1'];
// echo $sugg;
$term=$_POST['butt'];
//echo $term;
if($_POST['comm'])
{
$c=$_POST['comm'];
$result=mysql_query("SELECT * FROM suggestions WHERE term='$term' and suggestion='$sugg'");
while($row=mysql_fetch_array($result))
{
$oldcomm=$row['comments'];

}
$co=$c."</br>".$oldcomm;

mysql_query("UPDATE suggestions SET comments = '$co'
WHERE term = '$term'and suggestion='$sugg'");






echo "<table border='1'  cellspacing='1' cellpadding='2'  align='left' width='960px'>
<tr>
<th>Term</th>
<th>Domain</th>
<th>Language</th>
<th>Suggestion</th>
<th>Status</th>
<th>Suggested Date</th>
<th>Suggested By</th>
<th>Likes</th>
<th>Comments</th>
<th>Add Comments</th>
<th></th>
</tr>";
$result=mysql_query("SELECT * FROM suggestions WHERE term='$term'");
if(!$result) echo "invalid";
while($row=mysql_fetch_array($result))
{

echo "<form action='updatecommforsearchsuggestion.php' method='POST'>";
echo "<tr>";
echo "<td>".$row['term']."</td>";
$term=$row['term'];
echo "<td>".$row['domain']."</td>";

echo "<td>".$row['language']."</td>";
$sugg=$row['suggestion'];
echo "<td>".$row['suggestion']."<input type='hidden' name='butt1' value='$sugg'</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['suggested_date']."</td>";
echo "<td>".$row['suggested_by']."</td>";

echo "<td>".$row['votes']."</td>";
echo "<td>".$row['comments']."</td>";


echo "<td><textarea rows='1' cols='9' name='comm'></textarea><input type='submit' name='$term' value='add comment'/><input type='hidden' name='butt' value='$term'</td>";
echo "<td><a href=''>like</a></td>";

echo "<td><input type='submit' name='$term' value='Changestatus'/><input type='hidden' name='butt' value='$term'</td>";
echo "</tr>";
echo "</form>";
}
}



else
{




if($_POST['addsugg'])
{
$email=$_SESSION['email'];
 $result1 = mysql_query("SELECT * FROM system_user WHERE email='$email' ");
  if (mysql_num_rows($result1) == 1) 
  {
$su=$_POST['addsugg'];
	if($su=='2')
	{
	$result=mysql_query("SELECT * FROM suggestions WHERE term='$term'and suggestion='$sugg'");
	while($row=mysql_fetch_array($result))
{
//$term=$row['term'];
 
  $domain=$row['domain'];
  $language=$row['language'];
  $accepted_meaning=$row['suggestion'];
  $suggested_by=$row['suggested_by'];
  $suggested_date=$row['suggested_date'];
  $checked_by="admin";
  $accepted_date="";
  $votes=$row['votes'];
  $comments="";
  }
   $re=mysql_query("select * from terms where term='$term'");
  if (mysql_num_rows($re) == 1) 
  {
	while($row=mysql_fetch_array($re))
{
$acc=$row['accepted_meaning'];

}
$nacc=$acc."</br>".$accepted_meaning;

mysql_query("UPDATE terms SET accepted_meaning = '$nacc'
WHERE term = '$term'");
mysql_query("DELETE FROM suggestions WHERE term = '$term'and suggestion='$sugg'");
   echo   "<table width='960px' height='82' border='1' class='table' align='left'>";
   echo "<tr><td>Your Suggestion is added as Glossary, to <a href='home2.php'>View</a></td></tr>";
   echo "</table>";
  }
else
{
  $query="INSERT INTO terms (term, domain, accepted_meaning, language, suggested_by, suggested_date, checked_by, accepted_date,votes,comments) VALUES ('$term', '$domain', '$accepted_meaning', '$language', '$suggested_by', '$suggested_date', '$checked_by', '$accepted_date','$votes','$comments')";
  $rez=mysql_query($query);
  if(!$rez) {echo "INvalid".mysql_error();}
  mysql_query("DELETE FROM suggestions WHERE term = '$term'and suggestion='$sugg'");
   echo   "<table width='960px' height='82' border='1' class='table' align='left'>";
   echo "<tr><td>Your Suggestion is added as Glossary, to <a href='home2.php'>View</a></td></tr>";
   echo "</table>";
	}
	}
	
	
	
	
	
	
	if($su=='1')
	{
	mysql_query("UPDATE suggestions SET status = 'To Be Discussed'
WHERE term = '$term'and suggestion='$sugg'");

 echo   "<table width='960px' height='82' border='1' class='table' align='left'>";
   echo "<tr><td>Status is changed, to <a href='ToBeDiscussSugg.php'>Conform</a></td></tr>";
   echo "</table>";
	}
	
	}
	else
	{
	echo "<table border='1'  cellspacing='1' cellpadding='2'  align='left' width='960px'>
<tr><td>Your can't change the status(Only Allowed Administrator</td></tr></TABLE>";
	}
}











else
{
	
	$result=mysql_query("SELECT * FROM suggestions WHERE term='$term'and suggestion='$sugg'");
echo   "<table width='960px' height='82' border='1' class='table' align='left'>
<tr bgcolor='#FFFFFF'>
<th></th>

</tr>";
while($row=mysql_fetch_array($result))
{
echo "<form action='updatecommforsearchsuggestion.php' method='POST'>";
echo "<tr>";
$term=$row['term'];
echo "<td>Term:</td><td>".$row['term']."</td></tr>";

echo "<tr><td>Domain:</td><td>".$row['domain']."</td></tr>";
$sugg=$row['suggestion'];
echo "<tr><td>Suggestion:</td><td>".$row['suggestion']."<input type='hidden' name='butt1' value='$sugg'</td></tr>";
echo "<tr><td>Language:</td><td>".$row['language']."</td></tr>";
echo "<tr><td>Status:</td><td>".$row['status']."</td></tr>";
echo "<tr><td>Suggested by:</td><td>".$row['suggested_by']."</td></tr>";
echo "<tr><td>Suggested Date:</td><td>".$row['suggested_date']."</td></tr>";

echo "<tr><td>Number of Votes:</td><td>".$row['votes']."</td></tr>";
echo "<tr><td>Comments:</td><td>".$row['comments']."</td></tr>";
echo "<tr><td>Add Comments:</td><td><textarea rows='1' cols='9' name='comm'></textarea><input type='submit' name='$term' value='add comment'/><input type='hidden' name='butt' value='$term'</td></tr>";
//echo "<tr><td>Change Status:</td><td><textarea rows='1' cols='9' name='addsugg'></textarea><input type='submit' name='$term' value='Changestatus'/><input type='hidden' name='butt' value='$term'<input type='hidden' name='butt' value='$term'</td></tr>";
echo "<tr><td>Change Status:</td><td><Select name='addsugg'>
<Option value='0'> </Option>
										<Option value='1'>To Be Discussed</Option>
										<Option value='2'>accepted</Option>
										</Select><input type='submit' name='$term' value='Changestatus'/><input type='hidden' name='butt' value='$term'<input type='hidden' name='butt' value='$term'</td></tr>";

echo "</form>";
	
}	
}	
}
?>